<?php
include "conf.inc";

$op = isset($_POST["op"]) ? $_POST["op"] : "";
$ac = isset($_POST["ac"]) ? $_POST["ac"] : "";

$cate_handler = xoops_getmodulehandler("category");
switch ($op) {
    case "group":
        $group_handler = xoops_getmodulehandler("group");
        $perm_handler = xoops_getmodulehandler("permission");
        
        switch ($ac) {
            case "updatedesc":
                $msg = xoGroups::checkSubmitFields("group", $_POST, false);
                
                if ( true !== $msg ) {
                    xoGroups::redirect("group.php?id={$_POST["group_id"]}", 3, $msg);
                }
                
                $params = array(
                    "group_id"=>$_POST["group_id"],
                    "field"=> "group_description",
                    "value"=> $_POST["group_description"],
                );
                
                if (  $group_handler->updateGroupField($params,false) ) {
                    $msg = 'ok';
                } else {
					$msg = "no";
				}
				
				xoGroups::redirect("group.php?id={$_POST["group_id"]}",3,$msg);
                break;
                
            case "modify":
			
				break;
            case "create":
                $params = $_POST;
                $params["uid"] = $xoopsUser->uid();
                $params["group_creator"] = $xoopsUser->uname();
                
                $cat_id = isset($_POST["cat_id"]) ? intval($_POST["cat_id"]) : 0;

                // check create group fields 
                $msg = xoGroups::checkSubmitFields("group", $params, $token=true); 
                
                if ( true !== $msg ) {
                    xoGroups::redirect("index.php", 5, $msg);
                }
                
                if ( $group_id = $group_handler->setGroup($params) ) {
                    // create group default permission
                    $perm_handler->setGroupDefPerm($group_id);
                    // update category group total
                    $cate_handler->updateGroupTotal($cat_id);
                    // update group member
                    $group_handler->updateMemberTotal(array("group_id"=>$group_id,"value"=>1));
                    
                    $memb_handler = xoops_getmodulehandler("memb");
                    if ( $member_id = $memb_handler->setMember(array("group_id"=>$group_id,"uid"=>$xoopsUser->uid())) ) {
                        $memb_handler->updateToAdmin($member_id, true);
                    }
                    
                    // update group footprint
                    $footprint_handler = xoops_getmodulehandler("footprint");
                    $footprint_handler->setFootprint($params,true);
                    if ( $group_obj->admin() === true ) {
                        $params = array(
                            'footprint_display' => 1
                        );
                        $footprint_handler->updateFields($params);
                    }
                }
				
				if ( !empty($group_id) ) {
					//xoGroups::redirect("group.php?id={$group_id}",3,"设置权限!");
					header("Location: group.php?id={$group_id}");
				}
				xoGroups::redirect("create.php",3,"保存失败");
                break;
                
            default:
                die("not params");    
        }
        break;
        
    case "post":
        $threads_handler = xoops_getmodulehandler("threads");
        $posts_handler = xoops_getmodulehandler("posts");
        switch ($ac){
            case "modify":
                
                break;
                
            case "create":
                $params = $_POST;
                $mode = $_POST["mode"] ? trim($_POST["mode"]) : "";
                $params["threads_subject"] = $_POST["post_subject"];
                $params["uid"] = $xoopsUser->uid();
                $params["threads_author"] = $xoopsUser->uname();
                $params["threads_last_uid"] = $xoopsUser->uid();
                $params["threads_last_poster"] = $xoopsUser->uname();
                
                $params["post_author"] = $xoopsUser->uname(); 
                $params["post_user_sign"] = $xoopsUser->user_sig(); 
                $params["post_user_ip"] = ( isset($_SERVER['HTTP_X_FORWARDED_FOR']) ) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
                
				$msg = xoGroups::checkSubmitFields("posts", $_POST, false);
				
				$tid = isset($_POST["tid"]) ? intval($_POST["tid"]) : 0;
				
				if ( true !== $msg ) {
                    xoGroups::redirect("viewtopic.php?tid=".intval($_POST["tid"]), 5, $msg);
                }
                switch ($mode) {
                    case "edit":  
                    case "quote":
                    case "reply":
                        $params["post_first"] = 0;
                        break;
                        
                    case "post":
                    default:
                        if ( $tid = $threads_handler->setThread($params) ) {
                            $params["post_first"] = 1; 
                            $params["tid"] = $tid;
                        }
                        
                }
				
				$extra = "";
                if ( $post_id = $posts_handler->setPost($params) ) {
					$msg = "ok";
					$extra .= "&go=last&#last";
                } else {
					$msg = "on";
				}
				
				xoGroups::redirect("viewtopic.php?tid={$tid}{$extra}", 5, $msg);
                break;
            
            default:
                die("not params");  
        }
        break;

    default:
        die("not params");
}
?>